package cn.stylefeng.guns.modular.business.frequency.appversion.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.modular.business.custom.constant.PublicCommon;
import cn.stylefeng.guns.modular.business.frequency.appversion.entity.AppVersion;
import cn.stylefeng.guns.modular.business.frequency.appversion.enums.AppVersionExceptionEnum;
import cn.stylefeng.guns.modular.business.frequency.appversion.mapper.AppVersionMapper;
import cn.stylefeng.guns.modular.business.frequency.appversion.model.param.AppVersionParam;
import cn.stylefeng.guns.modular.business.frequency.appversion.model.result.AppVersionRespVo;
import cn.stylefeng.guns.modular.business.frequency.appversion.service.AppVersionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 版本表 服务实现类
 *
 * @author 李文龙
 * @date 2022/01/13 14:17
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class AppVersionServiceImpl extends ServiceImpl<AppVersionMapper, AppVersion> implements AppVersionService {

    @Override
    public PageResult<AppVersion> page(AppVersionParam appVersionParam) {

        // 构造条件
        LambdaQueryWrapper<AppVersion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppVersion::getDelFlag, PublicCommon.启用);
        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<AppVersion> list(AppVersionParam appVersionParam) {

        // 构造条件
        LambdaQueryWrapper<AppVersion> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }


    @Override
    public ResponseData add(AppVersionParam param) {
        AppVersion result = this.baseMapper.selectOne(Wrappers.<AppVersion>lambdaQuery().eq(AppVersion::getVersion, param.getVersion())
                .ne(AppVersion::getDelFlag, PublicCommon.删除));
        if (result != null) {
            return ResponseData.error("该版本已存在");
        }
        AppVersion entity = getEntity(param);
        entity.setDelFlag(PublicCommon.启用);
        int insertCount = this.baseMapper.insert(entity);
        if (insertCount == 0) {
            return ResponseData.error("添加失败");
        }
        return ResponseData.success();
    }

    private AppVersion getEntity(AppVersionParam param) {
        AppVersion entity = new AppVersion();
        BeanUtil.copyProperties(param, entity);
        return entity;
    }


    @Override
    public void delete(AppVersionParam appVersionParam) {
        AppVersion appVersion = baseMapper.selectById(appVersionParam.getId());
        appVersion.setDelFlag(PublicCommon.删除);
        baseMapper.updateById(appVersion);
    }


    @Override
    public ResponseData edit(AppVersionParam param) {
        AppVersion appVersion = this.baseMapper.selectById(param.getId());
        if (appVersion == null) {
            return ResponseData.error("该版本号不存在");
        }
        AppVersion appVersionResult = this.baseMapper.selectOne(Wrappers.<AppVersion>lambdaQuery().
                eq(AppVersion::getVersion, param.getVersion())
                .ne(AppVersion::getId, appVersion.getId())
                .ne(AppVersion::getDelFlag, PublicCommon.删除));
        if (appVersionResult != null) {
            return ResponseData.error("该版本号已存在");
        }
        appVersion.setVersion(param.getVersion());
        appVersion.setUrl(param.getUrl());
        appVersion.setIsForce(param.getIsForce());
        int updateCount = this.baseMapper.updateById(appVersion);
        if (updateCount == 0) {
            return ResponseData.error("修改失败");
        }
        return ResponseData.success();
    }


    @Override
    public AppVersion detail(AppVersionParam appVersionParam) {
        return this.queryAppVersion(appVersionParam);
    }

    @Override
    public ResponseData isUpgrade() {
        AppVersion appVersion = this.baseMapper.selectOne(Wrappers.<AppVersion>lambdaQuery().eq(AppVersion::getDelFlag, PublicCommon.启用)
                .orderByDesc(AppVersion::getVersion).last(" limit 1"));
        if (appVersion == null) {
            return ResponseData.success();
        }
        AppVersionRespVo appVersionRespVo = new AppVersionRespVo();
        appVersionRespVo.setVersion(appVersion.getVersion());
        appVersionRespVo.setUrl(appVersion.getUrl());
        appVersionRespVo.setIsForce(appVersion.getIsForce());
        return ResponseData.success(appVersionRespVo);
    }

    /**
     * 获取版本表
     *
     * @author 李文龙
     * @since 2022/01/13 14:17
     */
    private AppVersion queryAppVersion(AppVersionParam appVersionParam) {
        AppVersion appVersion = this.getById(appVersionParam.getId());
        if (ObjectUtil.isEmpty(appVersion)) {
            throw new ServiceException(AppVersionExceptionEnum.NOT_EXISTED);
        }
        return appVersion;
    }
}